﻿using System.Net;
using System.Text;
using System.IO;
using System.Collections.Specialized;
using System.Security.Authentication.ExtendedProtection;
using System.Security.Principal;
using System.Web.Routing;
using System.Threading;
namespace System.Web
{
    internal class NetHttpAsyncResult : IAsyncResult
    {
        private object _asyncState;
        private AsyncCallback _callback;
        private bool _completed;
        private bool _completedSynchronously;
        private Exception _error;
        private object _result;
        private RequestNotificationStatus _status;

        internal NetHttpAsyncResult(AsyncCallback cb, object state)
        {
            _callback = cb;
            _asyncState = state;
            _status = RequestNotificationStatus.Continue;
        }

        internal NetHttpAsyncResult(AsyncCallback cb, object state, bool completed, object result, Exception error)
        {
            _callback = cb;
            _asyncState = state;
            _completed = completed;
            _completedSynchronously = completed;
            _result = result;
            _error = error;
            _status = RequestNotificationStatus.Continue;
            if (_completed && (_callback != null))
                _callback(this);
        }

        internal void Complete(bool synchronous, object result, Exception error) { Complete(synchronous, result, error, RequestNotificationStatus.Continue); }
        internal void Complete(bool synchronous, object result, Exception error, RequestNotificationStatus status)
        {
            _completed = true;
            _completedSynchronously = synchronous;
            _result = result;
            _error = error;
            _status = status;
            if (_callback != null)
                _callback(this);
        }

        internal object End()
        {
            if (_error != null)
                throw new HttpException(null, _error);
            return _result;
        }

        internal void SetComplete()
        {
            _completed = true;
        }

        public object AsyncState
        {
            get { return _asyncState; }
        }

        public WaitHandle AsyncWaitHandle
        {
            get { return null; }
        }

        public bool CompletedSynchronously
        {
            get { return _completedSynchronously; }
        }

        internal Exception Error
        {
            get { return _error; }
        }

        public bool IsCompleted
        {
            get { return _completed; }
        }

        internal RequestNotificationStatus Status
        {
            get { return _status; }
        }
    }
}
